<?php include_once('Connections/tfccom_db.php'); ?>
<?php include_once('Connections/tfccom_select.php'); ?>
<?php
mysql_select_db($database_tfccom_db, $tfccom_db);
//daria 20120706
$strsort = "";

if(!isset($_GET['sort'])){
	$_GET['sort']=4;
}
switch($_GET['sort']){
	case '1':// Sq Ft
		$strsort = "square_feet";
		break;
	case '2'://Beds
		$strsort = "num_bedrooms";
		break;
	case '3'://Baths
		$strsort = "num_bathrooms";
		break;
	default://price
		$strsort = "price";
		break;
}
if($strsort!="price"){
	$strsort = " order by apartment.".$strsort." ASC , price asc ";
}else{
	$strsort = " order by apartment.".$strsort." ASC ";
}

if($_GET['p']=='a')
	$prices = "";
else
{
	$search_prices=explode("-",$_GET['p']);
	$prices = " AND price >= ".$search_prices[0]." AND price <= ".$search_prices[1];
}

if($_GET['n']=='a')
	$Neighborhood = "";
else
	$Neighborhood = " AND neighborhood_id LIKE '".$_GET['n']."'";

if($_GET['be']=='a')
	$Bedrooms = "";
else
	$Bedrooms = " AND num_bedrooms = ".$_GET['be'];

if($_GET['ba']=='a')
	$Bathrooms = "";
else
	$Bathrooms = " AND num_bathrooms = ".$_GET['ba'];

	
if($_GET['bu']=='a')
	$Building = "";
else
{
	$building = mysql_query($query_building, $tfccom_db) or die(mysql_error());
	$row_building = mysql_fetch_assoc($building);
	$i=0;
	do {
		$i++;
		$address=$row_building['address'];
	} while (($row_building = mysql_fetch_assoc($building))&&($_GET['bu']>$i));
	$Building = " AND building.address = '".$address."'";
}
if($_GET['am']=='a')
{
	$Amenities_1 = "";
	$Amenities_2 = "";
}
else
{
	$Amenities_1 = ", bldg_amenity_relation, amenity";
	$Amenities_2 = "AND bldg_amenity_relation.building_id = building.id AND bldg_amenity_relation.amenity_id=amenity.id AND amenity.id='".$_GET['am']."'";
}

if($_GET['a_s']=='a')
	$Apartment_Size = "";
else
	$Apartment_Size = " AND square_feet <= ".$_GET['a_s'];
	


$myQuery = sprintf("SELECT apartment.building_id, apartment.id,num_bedrooms, ROUND(num_bathrooms) AS num_bathrooms, apartment.apartment, apartment.price, apartment.square_feet, apartment.availability, building.neighborhood_id, building.name, building.contact_phone, building.contact_email, neighborhood.neighborhood,full_description, apt_type
FROM apartment, building, neighborhood %s
WHERE building.id = apartment.building_id AND neighborhood.id = building.neighborhood_id  AND apartment.availability =  '1' %s %s %s %s %s %s %s ".$strsort,$Amenities_1,$Neighborhood, $prices,$Bedrooms,$Bathrooms,$Building,$Apartment_Size,$Amenities_2);

$rsProperties = mysql_query($myQuery, $tfccom_db) or die(mysql_error());
$row_rsProperties = mysql_fetch_assoc($rsProperties);
$totalRows_rsProperties = mysql_num_rows($rsProperties);

$feature = mysql_query($query_feature, $tfccom_db) or die(mysql_error());
$row_feature = mysql_fetch_assoc($feature);

$href=$url_search_results."?p=".$_GET['p']."%26n=".$_GET['n']."%26be=".$_GET['be']."%26ba=".$_GET['ba']."%26bu=".$_GET['bu']."%26am=".$_GET['am']."%26a_s=".$_GET['a_s']."%26sort=".$_GET['sort'];
?>
<!DOCTYPE html>

<!--[if lt IE 7]>       <html class="ie6">  <![endif]-->
<!--[if IE 7]>          <html class="ie7">  <![endif]-->
<!--[if IE 8]>          <html class="ie8">  <![endif]-->
<!--[if gt IE 8]><!-->  <html lang='en-US'> <!--<![endif]-->

<head>
<meta charset='UTF-8' />
<meta name="viewport" content="width=640" />
<meta name="format-detection" content="telephone=no" />
<link rel="apple-touch-icon" href="iphone_icon.png" />
<link rel='shortcut icon' href='favicon.ico'>
<link rel='stylesheet' href='css/style.css'>
<title>TFCornerstone</title>
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">

$(function() {

$('.more').live("click",function() {
var last_msg_id = $(this).attr("id");
if(last_msg_id!='end'){
$.ajax({
type: "POST",
url: "search_results_more.php",
data: {'lastmsg': last_msg_id ,'sort': '<?php echo $_GET['sort']; ?>' ,'p': '<?php echo $_GET['p']; ?>','n': '<?php echo $_GET['n']; ?>','be': '<?php echo $_GET['be']; ?>','ba': '<?php echo $_GET['ba']; ?>','bu': '<?php echo $_GET['bu']; ?>','am':'<?php echo $_GET['am']; ?>','a_s':'<?php echo $_GET['a_s']; ?>','total':'<?php echo $totalRows_rsProperties; ?>'},
beforeSend:  function() {
$('a.more').html('<center><img src="img/loading.gif" /></center>');
  
},
success: function(html){
    $("#more").remove();
$("#search-results").append(html);
}
});
  
}
return false;
});

});
 
</script>
<script type="text/javascript">

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-9374994-1']);
_gaq.push(['_setDomainName', 'tfcornerstone.com']);
_gaq.push(['_trackPageview']);

(function() {
 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
 })();

</script>
</head>
<body class='search-results'>

<!-- ****** HEADER ****** -->
<div id='header'>

<div id='title'>
<a href='javascript:javascript:history.go(-1)' class='back-button'>Back</a>
<a href='tel:212.672.1000' class="phone-button-title">Call</a>
<p>Search results</p>
</div>
<a href="javascript:document.submitForm_2.submit();" class='refine-search-button'>Refine search</a>
<a href="javascript:document.submitForm.submit();" class='new-search-button'>New search</a>

</div><!-- #header -->
<form name="submitForm" action="home.php" method="POST" style="display:none">
<input type="hidden" name="p" value="<?php echo $_GET['p']; ?>" />
<input type="hidden" name="n" value="<?php echo $_GET['n']; ?>" />
</form>
<form name="submitForm_2" action="home.php?Go=2" method="POST" style="display:none">
<input type="hidden" name="p" value="<?php echo $_GET['p']; ?>" />
<input type="hidden" name="n" value="<?php echo $_GET['n']; ?>" />
<input type="hidden" name="be" value="<?php echo $_GET['be']; ?>" />
<input type="hidden" name="ba" value="<?php echo $_GET['ba']; ?>" />
<input type="hidden" name="bu" value="<?php echo $_GET['bu']; ?>" />
<input type="hidden" name="am" value="<?php echo $_GET['am']; ?>" />
<input type="hidden" name="a_s" value="<?php echo $_GET['a_s']; ?>" />
</form>
<!-- ****** CONTENT ****** -->
<form id="form1" name="form1" method="GET" action="search_results.php">
<input type="hidden" name="p" value="<?php echo $_GET['p']; ?>" />
<input type="hidden" name="n" value="<?php echo $_GET['n']; ?>" />
<input type="hidden" name="be" value="<?php echo $_GET['be']; ?>" />
<input type="hidden" name="ba" value="<?php echo $_GET['ba']; ?>" />
<input type="hidden" name="bu" value="<?php echo $_GET['bu']; ?>" />
<input type="hidden" name="am" value="<?php echo $_GET['am']; ?>" />
<input type="hidden" name="a_s" value="<?php echo $_GET['a_s']; ?>" />
  <select name="sort" id="sort" style="font-size:2em" onChange="submit();">
    <option value="a">Sort Option</option>
    <option value="1" <?php if (isset($_GET['sort'])&&($_GET['sort']=='1')) echo 'selected'; ?>>Sq Ft</option>
    <option value="2" <?php if (isset($_GET['sort'])&&($_GET['sort']=='2')) echo 'selected'; ?>>Beds</option>
    <option value="3" <?php if (isset($_GET['sort'])&&($_GET['sort']=='3')) echo 'selected'; ?>>Baths</option>
    <option value="4" <?php if (isset($_GET['sort'])&&($_GET['sort']=='4')) echo 'selected'; ?>>Price</option>
  </select>
</form>
<div id='content'>
<?php
if($totalRows_rsProperties!=0)
{
?>
<div id='search-results'>
<?php
$msg_id = '0';
$lastid = "0"; 
do
{
	$msg_id++;
	
$query_rsThumbnail = "SELECT * FROM images WHERE property_id = '".$row_rsProperties['building_id']."' AND category = 'building' AND id != '".$lastid."' ORDER BY RAND() LIMIT 0,1";
$rsThumbnail = mysql_query($query_rsThumbnail, $tfccom_db) or die(mysql_error());
$row_rsThumbnail = mysql_fetch_assoc($rsThumbnail);
$totalRows_rsThumbnail = mysql_num_rows($rsThumbnail);

$lastid = $row_rsThumbnail['id'];

$BuildFileName = $row_rsProperties['name'];
$BuildFileName = str_replace(" ", "-", $BuildFileName);
$BuildFileName = str_replace(".", "", $BuildFileName);
	
	
	
?>
<div class='listing'><a href="unit_page.php?id=<?php echo $row_rsProperties['id']; ?>"><?php if ($totalRows_rsThumbnail!=0) { ?><div style=" background:url(http://www.tfcornerstone.com/images/building/<?php echo $row_rsThumbnail['file_name'];?>) no-repeat 50% 0; background-position:50% 50%;width:292px; height:210px;overflow:hidden; float:left"></div><?php } else { ?><img src="http://www.tfcornerstone.com/images/no-image-thumb.gif" width="292" height="210" border="0" alt="" /><?php } ?><div class='listing-info'>
 
<p class='first-line'><?php echo($row_rsProperties['name']) ?></p><p><?php echo($row_rsProperties['neighborhood']) ?></p><p><?php
		if($row_rsProperties['num_bedrooms']==0)
		{
		  	echo 'Studio';
		}
		else
		{
		   echo($row_rsProperties['num_bedrooms']) ?> BD<?php    }?> / <?php echo($row_rsProperties['num_bathrooms']) ?> BA</p><p>$<?php echo(number_format($row_rsProperties['price'])) ?>&nbsp;<?php echo($row_rsProperties['apt_type']) ?></p>
</div></a>
<?php
$myQuery = "SELECT building.contact_phone 
FROM apartment, building 
WHERE building.id = apartment.building_id AND apartment.id='". $row_rsProperties['id'] ."'";
$rsPhone = mysql_query($myQuery, $tfccom_db) or die(mysql_error());
$row_rsPhone = mysql_fetch_assoc($rsPhone);
?>
<a href='tel:<?php echo $row_rsPhone['contact_phone']; ?>' class="phone2-button-title"><img src="img/phone2.gif" border="0"/></a>

</div><!-- .listing -->
 <?php } while (($row_rsProperties = mysql_fetch_assoc($rsProperties))&&($msg_id<5));
 
 if($totalRows_rsProperties!=$msg_id)
 {
 ?>
<div id="more" class="more-results"><a href='#' class="more" id="<?php echo $msg_id; ?>">More</a></div>
<?php
 }
?>
</div><!-- #search-results --><h2></h2>
<?php
}
else
{
?>
<div class="msg">Sorry, but no available apartments match your search criteria. <br>
Please try a New Search or check out these featured apartments.</div>
<h2>Featured listings</h2>

<?php
	do
	{
?>
    <div class='listing'><a href='unit_page.php?id=<?php echo $row_feature['id']; ?>'><div style=" background:url(http://www.tfcornerstone.com/images/building/<?php echo $row_feature['image'];?>) no-repeat 50% 0; background-position:50% 50%;width:292px; height:210px;overflow:hidden; float:left"></div>
<div class='listing-info'>
	<p class='first-line'><?php echo($row_feature['name']) ?></p><p><?php echo($row_feature['neighborhood']) ?></p><p><?php
		if($row_feature['num_bedrooms']==0)
		{
		  	echo 'Studio';
		}
		else
		{
		   echo($row_feature['num_bedrooms']) ?> BD<?php    }?> / <?php echo($row_feature['num_bathrooms']) ?> BA</p><p>$<?php echo(number_format($row_feature['price'])) ?>&nbsp;<?php echo($row_feature['apt_type']) ?></p>
</div></a>
<?php
$myQuery = "SELECT building.contact_phone 
FROM apartment, building 
WHERE building.id = apartment.building_id AND apartment.id='". $row_feature['id'] ."'";
$rsPhone = mysql_query($myQuery, $tfccom_db) or die(mysql_error());
$row_rsPhone = mysql_fetch_assoc($rsPhone);
?>
<a href='tel:<?php echo $row_rsPhone['contact_phone']; ?>' class="phone2-button-title"><img src="img/phone2.gif" border="0"/></a>
</div><!-- .listing -->
 <?php } while ($row_feature = mysql_fetch_assoc($feature));
}
 ?>

<div id='share'>
<a href='mailto:?subject=Check out this apartment listing from TF Cornerstone&body=<?php echo $href; ?>' class='email button'>Email info to a friend</a>
<a href='http://www.facebook.com/sharer.php?u=<?php echo $href; ?>&t=TFCornerstone' target="_blank" class='facebook button'>Share on Facebook</a>
<a href="https://twitter.com/share?text=I'm checking out this apt!" class="twitter button" data-dnt="true" data-count="none">Share on Twitter</a>
</div><!-- #share -->
</div><!-- #content -->
 <!-- ****** FOOTER ****** -->
<div id='footer'>

<a href="home.php"><div class='logo'><p>TF Cornerstone</p></div></a>
<div class='contacts'>
<p>387 Park Ave South, New York, NY 10016</p>
<p>MAIN: 212.672.1000 FAX: 212.672.1001</p>
</div>

<a class='full-site-button' href='http://www.tfcornerstone.com?desktop'>FULL SITE</a>

<div style="color:white; margin:20px 0 20px 0;overflow:auto">
          <div style="float:left;border-right:solid white;padding:0 30px 0 30px"><a href="home.php" style="font-size:28px;color:white">HOME</a></div>
          <div style="float:left;border-right:solid white;padding:0 30px 0 30px"><a href="about.php" style="font-size:28px;color:white">ABOUT</a></div>
          <div style="float:left;padding:0 0 0 30px"><a href="http://www.tfc.com/forms/privacy_policy.php?back=false" target="_blank" style="font-size:28px;color:white">DISCLAIMER</a></div>
</div>
<ul class='icons'>
<li class='google-plus'><a href="http://gplus.to/tfcornerstone" target="_blank">Google+</a></li>
<li class='twitter'><a href="http://twitter.com/#!/tfcornerstone" target="_blank">Twitter</a></li>
<li class='facebook'><a href="https://www.facebook.com/TFCrealestate" target="_blank">Facebook</a></li>
<li class='youtube'><a href="http://www.youtube.com/tfcornerstone" target="_blank">Youtube</a></li>
<li class='linked-in'><a href="http://www.linkedin.com/company/tf-cornerstone" target="_blank">Linked In</a></li>
<li class='rss'>
<?php
if(strstr($_SERVER['HTTP_USER_AGENT'],'Android')) { 
?>
	<a href="http://www.tfcornerstone.com/new-york-city-life/" target="_blank">RSS</a>
<?php	
} 
else { 
?>
	<a href="http://www.tfcornerstone.com/new-york-city-life/feed/" target="_blank">RSS</a>
<?php
}
 ?></li>
<li class='helper'></li>
</ul>

<p class='copyright'>&copy; 2012 <span>TF Cornerstone</span> INC</p>
<p class='copyright' style=" font-size:12px">EQUAL HOUSING OPPORTUNITY</p>
</div><!-- #footer -->
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
</body>
</html>
